package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Leave;
import cn.wolfcode.car.business.query.LeaveQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ILeaveService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 请假流程 控制器
 */
@Controller
// 注意：访问controller的路径可以 / 开头 或 不是/ 开头, 注意要跟管理填写对应的url保持一致
@RequestMapping("business/leave")
public class LeaveController {
    //模板前缀
    private static final String prefix = "business/leave/";

    @Autowired
    private ILeaveService leaveService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
//    @RequiresPermissions("system:leave:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }


//    @RequiresPermissions("system:leave:add")
    // 发起审核页面
    @RequestMapping("/addPage")
    public String addPage(Model model){

        // 审核流程: 根据类型获取
        BpmnInfo bpmnInfo = bpmnInfoService.selectByBpmnType("casual-leave");
        model.addAttribute("bpmnInfo", bpmnInfo);

        // 审核人（店长）
        List<User> directors = userService.queryByRoleKey("shopOwner");
        model.addAttribute("directors", directors);
        return prefix + "add";
    }

    //    @RequiresPermissions("system:leave:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Leave> query(LeaveQuery qo){
        return leaveService.query(qo);
    }

    //  发起审核 **********************************************
    @RequiresPermissions("system:leave:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Leave leave, Long bpmnInfoId){
        leaveService.save(leave, bpmnInfoId);
        return AjaxResult.success();
    }












    // 跳转 发起审核页面 -->  需要数据回显
//    @RequiresPermissions("system:leave:auditPage")
    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        // 服务单项
        Leave leave = leaveService.get(id);
        model.addAttribute("leave", leave);
        // 审核流程: 根据
        BpmnInfo bpmnInfo = bpmnInfoService.selectByBpmnType("car_package");
        model.addAttribute("bpmnInfo", bpmnInfo);
        // 审核人（店长）
        List<User> directors = userService.queryByRoleKey("shopOwner");
        model.addAttribute("directors", directors);
        // 审核人（财务）
        List<User> finances = userService.queryByRoleKey("financial");
        model.addAttribute("finances", finances);

        return prefix + "audit";
    }


    //数据-----------------------------------------------------------
    //列表




}
